package com.uetoken.cn.view.popwindow;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.uetoken.cn.R;
import com.uetoken.cn.bean.BlockchainPursesBean;
import com.uetoken.cn.utils.BlockchainUtils;
import com.uetoken.cn.utils.GlideApp;
import com.uetoken.cn.utils.Save2DecimalUtils;
import com.uetoken.cn.view.pagerecyclerview.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CurrencySelectPopWindow extends BasePopup {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.currencyRecyclerView)
    RecyclerView currencyRecyclerView;

    private Context mContext;
    private List<BlockchainPursesBean.DataBean> pursesList;
    private PursesAdapter mAdapter;
    private OnPurseListener mListener;
    private BlockchainPursesBean.DataBean pursesBean;
    private int mLastCheckedPosition = -1;
    private int position = 0;

    public CurrencySelectPopWindow(Context context, List<BlockchainPursesBean.DataBean> pursesList, int position) {
        super(context);
        mContext = context;
        this.pursesList = pursesList;
        this.position = position;
    }


    @Override
    protected void initAttributes() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pop_currency_sel, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//        int screenHeight = ScreenUtils.getScreenHeight();
//        // 设置SelectPicPopupWindow弹出窗体的高
//        this.setHeight((screenHeight / 10) * 7);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
    }

    @Override
    protected void initViews(View view) {
        mAdapter = new PursesAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, OrientationHelper.VERTICAL, false);
        currencyRecyclerView.setLayoutManager(layoutManager);
        currencyRecyclerView.addItemDecoration(new SpacesItemDecoration(0, 0, 31, 0));
        currencyRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setItemChecked(position);
                pursesBean = pursesList.get(position);
                if (mListener != null) {
                    mListener.onSelPurse(pursesBean);
                }
            }
        });
        setItemChecked(position);
    }

    public void setData(List<BlockchainPursesBean.DataBean> pursesList) {
        this.pursesList = pursesList;
        mAdapter.notifyDataSetChanged();
    }

    /**
     * @param position
     */
    public void setItemChecked(int position) {
        if (pursesList.size() <= 0)
            return;
        if (mLastCheckedPosition == position)
            return;
        mAdapter.getBooleanArray().put(position, true);
        if (mLastCheckedPosition > -1) {
            mAdapter.getBooleanArray().put(mLastCheckedPosition, false);
            mAdapter.notifyItemChanged(mLastCheckedPosition);
        }
        pursesBean = pursesList.get(position);
        mAdapter.notifyDataSetChanged();
        mLastCheckedPosition = position;
    }

    public interface OnPurseListener {
        void onSelPurse(BlockchainPursesBean.DataBean pursesBean);
    }

    public void setOnPurseListener(OnPurseListener listener) {
        mListener = listener;
    }

    public class PursesAdapter extends BaseQuickAdapter<BlockchainPursesBean.DataBean, BaseViewHolder> {

        private SparseBooleanArray mBooleanArray;

        public PursesAdapter() {
            super(R.layout.item_currency_view, pursesList);
            mBooleanArray = new SparseBooleanArray(pursesList.size());
        }

        public SparseBooleanArray getBooleanArray() {
            return mBooleanArray;
        }

        @Override
        protected void convert(BaseViewHolder helper, BlockchainPursesBean.DataBean item) {
            String purseName = item.getPursename();
            String balance = item.getBalance();
            int[] purseInfo = BlockchainUtils.getPurseIcon(purseName);
            int purseIcon = purseInfo[0];
            int newScale = purseInfo[1];

            if (mBooleanArray.get(helper.getAdapterPosition())) {
                helper.setVisible(R.id.pitchOnIv, true);
            } else {
                helper.setVisible(R.id.pitchOnIv, false);
            }
            GlideApp.with(mContext).load(ContextCompat.getDrawable(mContext, purseIcon)).into((ImageView) helper.getView(R.id.childPurseIv));
            helper.setText(R.id.childPurseNameTv, purseName);
            helper.setText(R.id.childPurseBalanceTv, mContext.getResources().getString(R.string.str_balance_2) + " " + balance);
        }
    }
}
